/** electron/main.ts */

import { app, BrowserWindow, globalShortcut } from 'electron';
import { join } from 'path';
import './ipc-render';

const createWindow = () => {
  const win = new BrowserWindow({
    minWidth: 1200,
    minHeight: 700,
    width: 1200,
    height: 700,
    frame: process.platform !== 'win32',
    titleBarStyle: 'customButtonsOnHover',
    hasShadow: process.platform !== 'darwin',
    webPreferences: {
      webviewTag: true,
      webSecurity: false,
      preload: join(__dirname, 'preload.js'),
    },
  });

  if (process.env.VITE_DEV_SERVER_URL) {
    win.loadURL(process.env.VITE_DEV_SERVER_URL);
  } else {
    win.loadFile(join(__dirname, '../dist/index.html'));
  }

  // 监听最大化
  win.on('maximize', () => {
    win.webContents.send('isMaximize', true);
  });
  // 监听最小化
  win.on('unmaximize', () => {
    win.webContents.send('isMaximize', false);
  });
};

app.whenReady().then(() => {
  createWindow();
});

app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') app.quit();
});

// 禁止快捷键
app.on('browser-window-focus', function () {
  globalShortcut.unregister('CommandOrControl+R');
  globalShortcut.unregister('F5');
  globalShortcut.unregister('Control+-');
});

// 解决屏幕设置150%后显示问题
app.commandLine.appendSwitch('high-dpi-support', '1');
app.commandLine.appendSwitch('force-device-scale-factor', '1');
